#ifndef READ_INCLUDED
#define READ_INCLUDED
#include <stdlib.h>
#include <iostream>
#include <string>
#include "Files.h"
#include "tinyxml.h"
#include "InpBean.h"


using namespace std;

class Read{
    public:
        Read():files(),inpbean(){}
        int ReadCommand(int argc,char *argv[]);
        int ReadInp();
    private:
        Files files;
        InpBean inpbean;
};

inline int Read::ReadCommand(int argc,char *argv[]){
    // --- default file names ---
    Files file;
    for(int i=0; i<argc ;i++){
        if(!strcmp(argv[i],"-i")){
            //inp=argv[i+1]; continue;
            file.set_inp(argv[i+1]); continue;
        }
        else if(!strcmp(argv[i], "-o")){
            //outp=argv[i+1];continue;
            file.set_outp(argv[i+1]);continue;
        }
        else if(!strcmp(argv[i], "-s")){
            //sufp=argv[i+1];continue;
            file.set_surp(argv[i+1]);continue;
        }
        else if(!strcmp(argv[i], "-vm")){
            //vmesh=argv[i+1];continue;
            file.set_vmesh(argv[i+1]);continue;
        }
        else if(!strcmp(argv[i], "-vp")){
            file.set_volp(argv[i+1]);continue;
        }else continue;
    }
    files = file;
    return 1;
}

inline int Read::ReadInp(){
    TiXmlDocument doc( "inp.xml" );
    bool loadOkay = doc.LoadFile();
    if ( !loadOkay ){
			printf( "Could not load test file 'inp.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
			exit( 1 );
    }
    TiXmlNode* root = doc.FirstChild( "root" );
    TiXmlElement* nmol = root->FirstChildElement("nmol");
    TiXmlElement* meshfmt = root->FirstChildElement("meshfmt");
    inpbean.nmol = atoi(nmol->FirstChild()->Value());
    inpbean.meshfmt = atoi(meshfmt->FirstChild()->Value());
    
    TiXmlElement* condition = root->FirstChildElement("condition");
    inpbean.di = atof(condition->FirstChildElement("di")->FirstChild()->Value());
    inpbean.de = atof(condition->FirstChildElement("de")->FirstChild()->Value());
    inpbean.ion_concentration = atof(condition->FirstChildElement("ion_concentration")->FirstChild()->Value());
    inpbean.temperature = atof(condition->FirstChildElement("temperature")->FirstChild()->Value());

    TiXmlElement* outputkey = root->FirstChildElement("outputkey");
    inpbean.ipotw = atoi(outputkey->FirstChildElement("ipotw")->FirstChild()->Value());
    inpbean.iforce = atoi(outputkey->FirstChildElement("iforce")->FirstChild()->Value());
    inpbean.iinterE = atoi(outputkey->FirstChildElement("iinterE")->FirstChild()->Value());
    inpbean.iselfE = atoi(outputkey->FirstChildElement("iselfE")->FirstChild()->Value());
    inpbean.ipotdx = atoi(outputkey->FirstChildElement("ipotdx")->FirstChild()->Value());
    
    TiXmlElement* files = root->FirstChildElement("files");
    inpbean.pqr = files->FirstChildElement("pqr")->FirstChild()->Value();
    inpbean.mesh = files->FirstChildElement("mesh")->FirstChild()->Value();
    return 1;
}

#endif
